package org.tools.wstt.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * Servlet implementation class ConfigServlet
 */
public class ProxyManageServlet extends AHttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		log("req uri : " + req.getRequestURI());
		RequestDispatcher vReqDispatcher = req
				.getRequestDispatcher(INTERNAL_PATH_OF_JSPS
						+ "/proxy_config.jsp");
		vReqDispatcher.forward(req, res);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log("POST on uri : " + request.getRequestURI());
		if (!StringUtils.isEmpty(request
				.getParameter(APP_CONFIG_PROXY_DEST_HOST))) {
			mContext.setAttribute(APP_CONFIG_PROXY_DEST_HOST, request
					.getParameter(APP_CONFIG_PROXY_DEST_HOST));
			log("destHost:"
					+ (String) mContext
							.getAttribute(APP_CONFIG_PROXY_DEST_HOST));
		}
		if (!StringUtils.isEmpty(request
				.getParameter(APP_CONFIG_PROXY_DEST_PORT))) {
			mContext.setAttribute(APP_CONFIG_PROXY_DEST_PORT, request
					.getParameter(APP_CONFIG_PROXY_DEST_PORT));
			log("destPort:"
					+ (String) mContext
							.getAttribute(APP_CONFIG_PROXY_DEST_PORT));
		}
		if (!StringUtils.isEmpty(request.getParameter("persistConf"))
				&& !StringUtils.isEmpty(System.getProperty("java.io.tmpdir"))) {
			try {
				// persistence required
				File vTempFile = new File(System.getProperty("java.io.tmpdir")
						+ "/" + APP_CONFIG_PREDEF_FILE);
				log("Configuration to save in : " + vTempFile.getAbsolutePath());
				vTempFile.delete();
				vTempFile.createNewFile();
				FileOutputStream out = new FileOutputStream(vTempFile);
				StringBuffer vSB = new StringBuffer();
				vSB.append(APP_CONFIG_PROXY_DEST_HOST);
				vSB.append("=");
				vSB.append(request.getParameter(APP_CONFIG_PROXY_DEST_HOST));
				vSB.append("\n");
				vSB.append(APP_CONFIG_PROXY_DEST_PORT);
				vSB.append("=");
				vSB.append(request.getParameter(APP_CONFIG_PROXY_DEST_PORT));

				out.write(vSB.toString().getBytes());
				out.close();
				log("Configuration saved to : " + vTempFile.getAbsolutePath());
			} catch (IOException vException) {
				logError("Error while persisting configuration", vException);
			}
		}
		// RequestDispatcher vReqDispatcher = request
		// .getRequestDispatcher(PATH_OF_JSPS + "config.jsp");
		// vReqDispatcher.forward(request, response);
		// better do a redirect to the page (allow refreshes of the page)
		response.sendRedirect(URI_TOOLS + "/proxy_config");
		return;
	}

}
